/*Console.java Interaction with the console of il2server
Copyright (C) 2013  Dmytro Kostiantynovskyi, HartLi

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Console {
	
	public Console(){
	}
	
	public void chat(String msg, int ownarmy, String pilotname) throws IOException{
		switch(ownarmy){
			default:
				ArrayList<String> lst = ioConsole("user "+pilotname);
				int army = Integer.parseInt(lst.get(1).substring(lst.get(1).indexOf("(")+1, lst.get(1).indexOf(")")));
				writeConsoleMsg("chat "+msg+" ARMY "+army);
				break;
			case 100:
				writeConsoleMsg("chat "+msg);
				break;
			case 101:
				if(pilotname!="")
					writeConsoleMsg("chat "+msg+" TO# "+pilotname);
				break;
			case 0:
				break;
		}
		
	}
	
	public boolean writeConsoleMsg(String msg) throws IOException{
		Socket sock = null;
		PrintWriter out = null;
		try {
			sock = new Socket(Config.serverIP4Address, Config.serverConsolePort);
            out = new PrintWriter(sock.getOutputStream(), true);
        } catch (UnknownHostException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
		if(out == null){
	        sock.close();
	        return false;
		}
			
		out.println(msg);
		out.flush();
        out.close();
        sock.close();
        return true;
	}
	
	public ArrayList<String> ioConsole(String msg) throws IOException{
		Socket sock = null;
	    PrintWriter out = null;
		BufferedReader in = null;
		try {
			sock = new Socket(Config.serverIP4Address, Config.serverConsolePort);
			out = new PrintWriter(sock.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		   
		} catch (UnknownHostException e) {
		   System.err.println(e.getMessage());
		   System.exit(1);
		} catch (IOException e) {
		   System.err.println(e.getMessage());
		   System.exit(1);
		}
		ArrayList<String> lstOutput = new ArrayList<String>();
		String userInput;
		out.println(msg);
		while((userInput = in.readLine()) != null) {
			lstOutput.add(userInput);
		    Pattern p = Pattern.compile("<console.*");
		    Matcher m = p.matcher(userInput);
		    if(m.find())
		    	break;
		}
		in.close();
		sock.close();
		return lstOutput;
	}
}
